package main

import (
	"flag"
	"fmt"
	"shop/common/log"
	"shop/config"
	"shop/dao"
	"shop/midware"
	"shop/monitor"
	"shop/timer"
	"shop/utils/convertutil"
	"shop/utils/tcentcloudutil"
	"time"

	"github.com/gin-gonic/gin"
)

var startTime = convertutil.FormatStandardDate(time.Now())
var serverPort = "18888"

func main() {
	flag.StringVar(&serverPort, "port", "18888", "http port")
	flag.Parse()

	// init config
	config.Init()

	// init log
	if config.IsProdEnv() {
		log.InitLogger("log/shop.log")
		fmt.Println("logfile init done")
	}

	// init db, redis
	dao.InitMysql()
	dao.InitRedis()

	// init util
	tcentcloudutil.InitCosClient()
	tcentcloudutil.InitSmsClient()

	// 开启定时任务
	timer.StartTimerTask()

	// gin框架日志配置
	gin.SetMode(gin.ReleaseMode)
	engine := gin.New()
	// 全局中间件
	engine.Use(midware.Recover, gin.LoggerWithWriter(log.Config.LogWriter), midware.Cors)
	// 路由设置
	setRouters(engine)
	setAdminRouters(engine)

	// 初始化监控
	monitor.Init()

	log.Infof("当前是否是生产环境: %v, 使用端口: %s", config.IsProdEnv(), serverPort)
	if err := engine.Run(":" + serverPort); err != nil {
		panic(err)
	}
}
